#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- This"s Web Forms
# -- 
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2018年2月13日 13:40:21
# ----------------------------------------------------------
from flask_babel import gettext
from flask_wtf import FlaskForm as Form
from wtforms import StringField, BooleanField, TextAreaField
from wtforms.validators import DataRequired, Length
from core.models.user_model import User


class LoginForm(Form):
    """
        Login Form

        :Author: lmay.Zhou
        :Date: 2018年2月13日 13:40:21
    """
    openid = StringField("openid", validators=[DataRequired()])
    remember_me = BooleanField("remember_me", default=False)


class EditForm(Form):
    """
        Edit Form

        :Author: lmay.Zhou
        :Date: 2018年2月28日 15:36:16
    """
    nickname = StringField("nickname", validators=[DataRequired()])
    about_me = TextAreaField("about_me", validators=[Length(min=0, max=140)])

    def __init__(self, original_nickname, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.original_nickname = original_nickname

    def validate(self):
        if not Form.validate(self):
            return False
        if self.nickname.data == self.original_nickname:
            return True
        if self.nickname.data != User.make_valid_nickname(self.nickname.data):
            self.nickname.errors.append(gettext("This nickname has invalid characters. Please use letters, numbers, dots and underscores only."))
            return False
        user = User.query.filter_by(nickname=self.nickname.data).first()
        if user:
            self.nickname.errors.append(gettext("This nickname is already in use. Please choose another one."))
            return False
        return True
